const { Sequelize, DataTypes } = require("sequelize");

// 从环境变量中读取数据库配置
const { MYSQL_ADDRESS = "sh-cynosdbmysql-grp-cqjrknx2.sql.tencentcdb.com:24853" } = process.env;
// const MYSQL_ADDRESS = "sh-cynosdbmysql-grp-cqjrknx2.sql.tencentcdb.com:24853";
const [host, port] = MYSQL_ADDRESS.split(":");
const MYSQL_USERNAME = 'root'
const MYSQL_PASSWORD = '040124Wang@'
// const MYSQL_PASSWORD = '123456'
// const host = 'https://sh-cynosdbmysql-grp-cqjrknx2.sql.tencentcdb.com'
// const port = '24853'
const sequelize = new Sequelize("jyuTW", MYSQL_USERNAME, MYSQL_PASSWORD, {
  host: host,
  port: port,
  dialect: "mysql" /* one of 'mysql' | 'mariadb' | 'postgres' | 'mssql' */,
});

// 定义数据模型
const Counter = sequelize.define("Counter", {
  count: {
    type: DataTypes.INTEGER,
    allowNull: false,
    defaultValue: 1,
  },
});

const User = sequelize.define("User", {
  username: {
    type: DataTypes.STRING,
    allowNull: false,
  },
  password: {
    type: DataTypes.STRING,
    allowNull: false,
  },
  openId: {
    type: DataTypes.STRING,
    allowNull: false,
  },
  avatarUrl: {
    type: DataTypes.STRING,
    allowNull: false,
  },
  postNums: {
    type: DataTypes.INTEGER,
    allowNull: false,
    defaultValue: 0,
  },
  role: {
    type: DataTypes.STRING,
    allowNull: false,
    defaultValue: 'user'
  }
})

const PostItem = sequelize.define("PostItem", {
  content: {
    type: DataTypes.STRING,
    allowNull: false,
  },
  title: {
    type: DataTypes.STRING,
    allowNull: true,
  },
  date: {
    type: DataTypes.STRING,
    allowNull: false,
  },
  likeNums: {
    type: DataTypes.INTEGER,
    allowNull: true,
    defaultValue: 0,
  },
  type: {
    //0,1,2,3
    type: DataTypes.STRING,
    allowNull: false,
  },
  statu: {
    type: DataTypes.INTEGER,
    allowNull: false,
  },
  commentNums: {
    type: DataTypes.INTEGER,
    allowNull: true,
    defaultValue: 0,
  },
  imgUrls: {
    type: DataTypes.JSON,
    defaultValue: []
  },
  clubId: {
    type: DataTypes.INTEGER,
    allowNull: true,
  },
  teacherId: {
    type: DataTypes.INTEGER,
    allowNull: true,
  }
})

const CommentItem = sequelize.define("CommentItem", {
  content: {
    type: DataTypes.STRING,
    allowNull: false,
  },
  date: {
    type: DataTypes.STRING,
    allowNull: false,
  },
  likeNums: {
    type: DataTypes.INTEGER,
    allowNull: true,
    defaultValue: 0,
  },
  postId: {
    type: DataTypes.INTEGER,
    allowNull: true,
  },
  parentId: {
    type: DataTypes.INTEGER,
    allowNull: true,
  }
})

const Club = sequelize.define("Club", {
  clubName: {
    type: DataTypes.STRING,
    allowNull: false,
  },
  clubLogo: {
    type: DataTypes.STRING,
    allowNull: false,
  },
  clubIntro: {
    type: DataTypes.STRING,
    allowNull: false,
  },
  postNums: {
    type: DataTypes.INTEGER,
    allowNull: false,
    defaultValue: 0,
  },
  founder: {
    type: DataTypes.STRING,
    allowNull: true,
  },
  head: {
    type: DataTypes.STRING,
    allowNull: false,
  },
  headPhone: {
    type: DataTypes.STRING,
    allowNull: false,
  },
  statu: {
    type: DataTypes.INTEGER,
    allowNull: false,
    defaultValue: 0,//0,1,2
  },
  memberNums: {
    type: DataTypes.INTEGER,
    allowNull: false,
    defaultValue: 0,
  },
  clubDetailBackground: {
    //详情背景图片
    type: DataTypes.STRING,
    allowNull: true,
  }
})


const JoinClub = sequelize.define("JoinClub", {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true
  },
  statu: {
    type: DataTypes.INTEGER,
    allowNull: false,
    defaultValue: 0,//0,1,2
  },
  // clubId: {
  //   type: DataTypes.INTEGER,
  //   allowNull: false,
  // },
  // userId: {
  //   type: DataTypes.INTEGER,
  //   allowNull: false,
  // },
  role: {
    type: DataTypes.STRING,
    allowNull: false,
    defaultValue: 'member' // club_admin
  }
})

User.hasMany(PostItem); // 用户拥有多个帖子
PostItem.belongsTo(User); // 帖子属于一个用户

PostItem.belongsTo(Club, { foreignKey: { name: 'clubId', allowNull: true } }); // PostItem 可以选择性地关联到 Club
Club.hasMany(PostItem, { foreignKey: { name: 'clubId', allowNull: true } }); // Club 可以拥有多个 PostItem

User.hasMany(CommentItem);//用户拥有多个评论
CommentItem.belongsTo(User);//评论属于一个用户


JoinClub.belongsTo(User, { foreignKey: 'userId' });
JoinClub.belongsTo(Club, { foreignKey: 'clubId' });

Club.belongsToMany(User, { through: 'JoinClub', foreignKey: 'clubId' })
User.belongsToMany(Club, { through: 'JoinClub', foreignKey: 'userId' })

// 数据库初始化方法
async function init() {
  await Counter.sync({ alter: true });
  await User.sync({ alter: true });
  await PostItem.sync({ alter: true });
  await CommentItem.sync({ alter: true });
  await Club.sync({ alter: true });
  await JoinClub.sync({ alter: true });
}

// 导出初始化方法和模型
module.exports = {
  init,
  Counter,
  User,
  PostItem,
  CommentItem,
  Club,
  JoinClub
};
